import json
from typing import List
from model.hotfix_version import HotfixVersion
import os


class HotfixModule:
    def __init__(self, name, version: HotfixVersion, launch_code: str):
        self.name = name
        self.version = version
        self.launch_code = launch_code

    def module_path(self):
        return f"./modules/{self.name}/{self.version}/"

    def zip_path(self):
        return f"./assets/{self.name}-{self.version}.zip"

    def zip_url(self):
        return f"/assets/{self.name}-{self.version}.zip"

    def requirements(self) -> List[str]:
        lines = []

        file = self.module_path() + "requirements.txt"

        if not os.path.exists(file):
            return lines

        with open(file, "r") as f:
            lines = f.readlines()
            # 去除\n与空行
            lines = [line.strip() for line in lines if line.strip() != ""]

        return lines

    def __str__(self):
        return f"{self.name}-{self.version}"
